<?php
/**
 * 《银行帐号》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class BankMy extends EmpUtil {
	public $tbl = Constant::tbl_emp_bank;
	
	public $bank = array(
		'-' => '请选择',			
		'招商银行' => '招商银行',
		'中信银行' => '中信银行',
		'中国工商银行' => '中国工商银行',
		'东莞农村商业银行' => '东莞农村商业银行',
		'中国民生银行' => '中国民生银行',
			
		'广发银行' => '广发银行',
		'平安银行' => '平安银行',
		'北京银行' => '北京银行',
		'汇丰银行' => '汇丰银行',
	
		// '5' => '东莞银行',
		'9' => '其它',
	);
	
	public $service = array(
		'-' => '请选择',
		'1' => '工资',
		'2' => '借款',
		'3' => '报销',
		'4' => '付款',
	
		'5' => '个人',

		'9' => '其它',
	);
	
	public $type = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '单位',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
	
		'bank' => '开户行',
		'name' => '户名',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('service_option', $this->service);
		
		$controller->assign_by_ref('bank_option', $this->bank);
		
		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('status_s', '1');
		
		$controller->display($request, 'emp/bank/my/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {	
		$bank_s = $request['bank_s'];
		$name_s = $request['name_s'];
				
		$where = " WHERE 1=1";

		if (!empty($bank_s) && $bank_s <> '-') {
			$where .= " AND bank LIKE '" . $bank_s . "%'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}

		$where .= $this->_where_x("type", $request['type_s']);
		
		$where .= $this->_where_x("service", $request['service_s']);

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
								
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/bank/my/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/bank/my/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$service = $request['service'];
		$type = $request['type'];
		
		$bank = $request['bank'];		
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		$city = $request['city'];
		$zone = $request['zone'];
		
		$name = addslashes($request['name']);
			
		$code = $request['code'];
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, service, type, bank, branch, bankcode, city, zone, name, code, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $service, $type, $bank, $branch, $bankcode, $city, $zone, $name, $code, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/bank/my/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/bank/my/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$service = $request['service'];
		$type = $request['type'];
		
		$bank = $request['bank'];
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		$city = $request['city'];
		$zone = $request['zone'];
			
		$name = addslashes($request['name']);
		$code = $request['code'];
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$status = $request['status'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', service = '%s', type = '%s', bank = '%s', branch = '%s', bankcode = '%s', city = '%s', zone = '%s', ";
		$fmt .= "name = '%s', code = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $service, $type, $bank, $branch, $bankcode, $city, $zone, $name, $code, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/bank/my/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['service_t'] = $this->service[$row['service']];
		$row['type_t'] = $this->type[$row['type']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function t($controller, $request) {

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
			
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function ajax_id($controller, $request) {
		$q = $request['q'];
		$data = $this->id($this->tbl, $q);		
		$data['code'] = $this->_format($data['code'], 4, ' ');
		
		echo json_encode($data);
	}
	
	function _bank($emp, $service, $bank = '招商银行', $type = '1') {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND service = '%s' AND bank = '%s' AND type = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $service, $bank, $type);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
		
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$aa = new Bank();
		
		return $aa->_bank_primary("1", $emp);
	}
	
	function ajax_my($controller, $request) {
		$emp = $_SESSION['id'];
		
		$service = $request['service'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND service = '%s' AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $service, $emp);
		debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$data[] = $row;
		}
	
		echo json_encode($data);
	}
}
?>